package com.zhaoyang.servlet;

import com.zhaoyang.dao.AdminDao;
import com.zhaoyang.dao.StudentDao;
import com.zhaoyang.dao.TeacherDao;
import com.zhaoyang.model.Admin;
import com.zhaoyang.model.Student;
import com.zhaoyang.model.Teacher;
import com.zhaoyang.util.SHAUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 赵扬
 * @time 2020/4/28 13:03
 */
@WebServlet("/systemServlet")
public class SystemServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String method = request.getParameter("method");
        if ("toPersonalView".equals(method)) {
            personalView(request, response);
            return;
        } else if ("EditPasswod".equals(method)) {
            editPassword(request, response);
            return;
        }
        request.getRequestDispatcher("./view/system.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    /**
     * 修改用户密码
     *
     * @param request
     * @param response
     */
    private void editPassword(HttpServletRequest request, HttpServletResponse response) {
        //设置编码
        response.setCharacterEncoding("UTF-8");
        // 将密码加密
        String password = SHAUtil.encode(request.getParameter("password"));
        String newPassword = request.getParameter("newpassword");

        int userType = Integer.parseInt(request.getSession().getAttribute("userType").toString());
        if (1 == userType) {
            //管理员
            Admin admin = (Admin) request.getSession().getAttribute("user");

            if (!admin.getPassword().equals(password)) {
                try {
                    response.getWriter().write("原密码错误");
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            AdminDao adminDao = new AdminDao();
            if (adminDao.editPasswordById(admin.getId(), newPassword)) {
                try {
                    response.getWriter().write("success");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    response.getWriter().write("数据库修改错误");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (userType == 2) {
            //学生
            Student student = (Student) request.getSession().getAttribute("user");
            if (!student.getPassword().equals(password)) {
                try {
                    response.getWriter().write("原密码错误！");
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            StudentDao studentDao = new StudentDao();
            if (studentDao.editPassword(student.getId(), newPassword)) {
                try {
                    response.getWriter().write("success");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    response.getWriter().write("数据库修改错误");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        if (userType == 3) {
            //教师
            Teacher teacher = (Teacher) request.getSession().getAttribute("user");
            if (!teacher.getPassword().equals(password)) {
                try {
                    response.getWriter().write("原密码错误!");
                    return;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            TeacherDao teacherDao = new TeacherDao();
            if (teacherDao.editPassword(teacher, newPassword)) {
                try {
                    response.getWriter().write("success");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    response.getWriter().write("数据库修改错误");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    private void personalView(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getRequestDispatcher("view/personalView.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
